/**
 * @作者 Wang Sicheng
 * @时间 2019年7月22日20:31:24
 */
package com.chinaunicom.tour.sys.controller;

import com.chinaunicom.tour.sys.constant.ResultConstants;
import com.chinaunicom.tour.sys.entity.Stragegy;
import com.chinaunicom.tour.sys.entity.StragegyCollect;
import com.chinaunicom.tour.sys.entity.User;
import com.chinaunicom.tour.sys.model.StragegyCollectModel;
import com.chinaunicom.tour.sys.model.StragegyModel;
import com.chinaunicom.tour.sys.service.StragegyCollectService;
import com.chinaunicom.tour.sys.service.StragegyService;
import com.chinaunicom.tour.sys.util.ObjectRestResponse;
import com.chinaunicom.tour.sys.util.TableResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(value = "攻略收藏管理", tags = { "攻略收藏管理" })
@RestController
@RequestMapping("stragegyCollect")
public class StragegyCollectController {

    @Autowired
    StragegyCollectService stragegyCollectService;
    @ApiOperation(value = "收藏或取消收藏", notes = "")
    @RequestMapping(value = "/{stragegyId}", method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<StragegyCollect> collect(@PathVariable String stragegyId, HttpServletRequest request) {
        ObjectRestResponse<StragegyCollect> orr = new ObjectRestResponse<StragegyCollect>();
        orr.setData(null);
        try {
        	////////////////////此处获取用户ID，获取不到则是未登录////////////////
            User user;
            HttpSession session = request.getSession();
            /////////////////////临时模拟登录////////////////////////////
//            User usertemp=new User();
//            usertemp.setId("1");
//            session.setAttribute("user",usertemp);
            /////////////////////////!!!!!!!!!!//////////////////////////
            user=(User)session.getAttribute("user");
            String userid=null;
            if(!(user==null)){
                userid=user.getId();
            }
            if (userid ==null || userid.equals("")) {
                orr.setStatus(ResultConstants.ERROR_CODE);
                orr.setMessage("用户未登录");
                return orr;
            }
            ///////////////////////////////////////////////////////////////////////
            StragegyCollectModel queryModel=new StragegyCollectModel();
            queryModel.setUserId(userid);
            queryModel.setStragegyId(stragegyId);
            int flag=stragegyCollectService.isCollected(queryModel);
            if(flag>0){//已经收藏，执行删除
                stragegyCollectService.delete(queryModel);
                orr.setStatus(ResultConstants.SUCCESS_CODE);
                orr.setMessage("取消收藏成功");

            }else{//未收藏，执行收藏
                StragegyCollect stragegyCollect=new StragegyCollect();
                stragegyCollect.setUserId(userid);
                stragegyCollect.setStragegyId(stragegyId);
                stragegyCollect.setCreateTime(new Date());
                stragegyCollect.setId(null);
                stragegyCollectService.add(stragegyCollect);
                //orr.setStatus(ResultConstants.SUCCESS_CODE);
                orr.setStatus(201);
                orr.setMessage("收藏成功");

            }
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("执行失败" + e.getMessage());
            return orr;
        }
    }

    @ApiOperation(value = "分页查询", notes = "")
    @RequestMapping(value = "/pageQuery", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Stragegy> pageQuery(@ModelAttribute StragegyCollectModel queryModel) {
        TableResultResponse<Stragegy> trr = new TableResultResponse<Stragegy>();

        try {
            List<Stragegy> list = stragegyCollectService.pageQuery(queryModel);
            trr.getData().setTotal(stragegyCollectService.pageQueryCount(queryModel));
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }
    @ApiOperation(value = "查询所有", notes = "")
    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<Stragegy> findList(@ModelAttribute StragegyCollectModel queryModel) {
        TableResultResponse<Stragegy> trr = new TableResultResponse<Stragegy>();
        try {
            List<Stragegy> list = stragegyCollectService.findList(queryModel);
            trr.getData().setTotal(list.size());
            trr.getData().setRows(list);
            trr.setStatus(ResultConstants.SUCCESS_CODE);
            trr.setMessage("查询成功");
            return trr;
        } catch (Exception e) {
            e.printStackTrace();
            trr.setStatus(ResultConstants.ERROR_CODE);
            trr.setMessage("查询失败" + e.getMessage());
            trr.getData().setTotal(0);
            trr.getData().setRows(null);
            return trr;
        }
    }

    @ApiOperation(value = "根据id查询", notes = "")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<StragegyCollect> findById(@PathVariable String id) {
        ObjectRestResponse<StragegyCollect> orr = new ObjectRestResponse<StragegyCollect>();

        try {
            StragegyCollect stragegyCollect = stragegyCollectService.findById(id);
            orr.setData(stragegyCollect);
            orr.setStatus(ResultConstants.SUCCESS_CODE);
            orr.setMessage("查询成功");
            return orr;
        } catch (Exception e) {
            e.printStackTrace();
            orr.setStatus(ResultConstants.ERROR_CODE);
            orr.setMessage("查询失败" + e.getMessage());
            return orr;
        }
    }
}
